{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n# Styling with cycler\n\n\nDemo of custom property-cycle settings to control colors and other style\nproperties for multi-line plots.\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>More complete documentation of the ``cycler`` API can be found\n    `here <http://matplotlib.org/cycler/>`_.</p></div>\n\nThis example demonstrates two different APIs:\n\n1. Setting the default rc parameter specifying the property cycle.\n   This affects all subsequent axes (but not axes already created).\n2. Setting the property cycle for a single pair of axes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from cycler import cycler\nimport numpy as np\nimport matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First we'll generate some sample data, in this case, four offset sine\ncurves.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.linspace(0, 2 * np.pi, 50)\noffsets = np.linspace(0, 2 * np.pi, 4, endpoint=False)\nyy = np.transpose([np.sin(x + phi) for phi in offsets])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now ``yy`` has shape\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(yy.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So ``yy[:, i]`` will give you the ``i``-th offset sine curve. Let's set the\ndefault prop_cycle using :func:`matplotlib.pyplot.rc`. We'll combine a color\ncycler and a linestyle cycler by adding (``+``) two ``cycler``'s together.\nSee the bottom of this tutorial for more information about combining\ndifferent cyclers.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "default_cycler = (cycler(color=['r', 'g', 'b', 'y']) +\n                  cycler(linestyle=['-', '--', ':', '-.']))\n\nplt.rc('lines', linewidth=4)\nplt.rc('axes', prop_cycle=default_cycler)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we'll generate a figure with two axes, one on top of the other. On the\nfirst axis, we'll plot with the default cycler. On the second axis, we'll\nset the prop_cycler using :func:`matplotlib.axes.Axes.set_prop_cycle`\nwhich will only set the ``prop_cycle`` for this :mod:`matplotlib.axes.Axes`\ninstance. We'll use a second ``cycler`` that combines a color cycler and a\nlinewidth cycler.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "custom_cycler = (cycler(color=['c', 'm', 'y', 'k']) +\n                 cycler(lw=[1, 2, 3, 4]))\n\nfig, (ax0, ax1) = plt.subplots(nrows=2)\nax0.plot(yy)\nax0.set_title('Set default color cycle to rgby')\nax1.set_prop_cycle(custom_cycler)\nax1.plot(yy)\nax1.set_title('Set axes color cycle to cmyk')\n\n# Add a bit more space between the two plots.\nfig.subplots_adjust(hspace=0.3)\nplt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting ``prop_cycler`` in the ``matplotlibrc`` file or style files\n-------------------------------------------------------------------\n\nRemember, if you want to set a custom ``prop_cycler`` in your\n``.matplotlibrc`` file or a style file (``style.mplstyle``), you can set the\n``axes.prop_cycle`` property:\n\n.. code-block:: python\n\n   axes.prop_cycle    : cycler(color='bgrcmyk')\n\nCycling through multiple properties\n-----------------------------------\n\nYou can add cyclers:\n\n.. code-block:: python\n\n   from cycler import cycler\n   cc = (cycler(color=list('rgb')) +\n         cycler(linestyle=['-', '--', '-.']))\n   for d in cc:\n       print(d)\n\nResults in:\n\n.. code-block:: python\n\n    {'color': 'r', 'linestyle': '-'}\n    {'color': 'g', 'linestyle': '--'}\n    {'color': 'b', 'linestyle': '-.'}\n\n\nYou can multiply cyclers:\n\n.. code-block:: python\n\n    from cycler import cycler\n    cc = (cycler(color=list('rgb')) *\n          cycler(linestyle=['-', '--', '-.']))\n    for d in cc:\n        print(d)\n\nResults in:\n\n.. code-block:: python\n\n    {'color': 'r', 'linestyle': '-'}\n    {'color': 'r', 'linestyle': '--'}\n    {'color': 'r', 'linestyle': '-.'}\n    {'color': 'g', 'linestyle': '-'}\n    {'color': 'g', 'linestyle': '--'}\n    {'color': 'g', 'linestyle': '-.'}\n    {'color': 'b', 'linestyle': '-'}\n    {'color': 'b', 'linestyle': '--'}\n    {'color': 'b', 'linestyle': '-.'}\n\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
